package acwing._1_1AlgorithmBasic._5DynamicProgramming;
/**
 * @Project : AlgorithmLearning
 * @Package : ACWing._1_1AlgorithmBasic._5DynamicProgramming
 * @File : Q0897.java
 * @Author : WangRuoyu
 * @Date : 2023/4/6 14:58
 */

import java.io.*;

public class Q0897 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String[] words = br.readLine().split(" ");
        String a = br.readLine();
        String b = br.readLine();

        int[][] f = new int[a.length() + 1][b.length() + 1];
        for (int i = 1; i <= a.length(); ++i) {
            for (int j = 1; j <= b.length(); ++j) {
                f[i][j] = Math.max(f[i - 1][j], f[i][j - 1]);
                if (a.charAt(i - 1) == b.charAt(j - 1)) f[i][j] = Math.max(f[i][j], f[i - 1][j - 1] + 1);
            }
        }

        System.out.println(f[a.length()][b.length()]);
    }
}

